import {Injectable} from '@angular/core';
import {UrlApi} from '../../../frame';
import {FileUploader} from 'ng2-file-upload';

@Injectable()
export class AliOSSService {

    simple = (): {
        fileUrl: string,
        uploader: FileUploader,
        uploadFile: (onSuccess) => void
    } => {
        const oss = {
            fileUrl: '',
            // 初始化定义uploader变量,用来配置input中的uploader属性
            uploader: new FileUploader({
                url: UrlApi.aliOSS.uploadObject,
                method: 'POST',
                itemAlias: 'file',
                removeAfterUpload: true
            }),
            // 定义事件，上传文件
            uploadFile: (onSuccess) => {
                // 上传
                oss.uploader.queue[0].onSuccess = onSuccess;
                oss.uploader.queue[0].upload(); // 开始上传
            }
        };
        return oss;
    }

    constructor() {
    }

}
